<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/9/15
 * Time: 14:44
 */

namespace app\admin\controller;

use app\admin\service\UserService;
use app\admin\validator\UserValidator;
use lib\common\CommonController;
use lib\annotations\Permission;

class User extends CommonController
{
    /**
     * @Permission
     * @param UserService $service
     * @return void
     */
    public function index (UserService $service): void {
        $params = $this->request->all();
        $data = $service->getList($params);
        $this->success($data);
    }

    /**
     * @Permission
     * @param UserService $service
     * @return void
     */
    public function create (UserService $service): void {
        $params = $this->request->all();
        (new UserValidator())->goCheck($params, 'create');
        $service->create($params);
        $this->success();
    }

    /**
     * @Permission
     * @param $id
     * @param UserService $service
     * @return void
     */
    public function update ($id, UserService $service): void {
        $params = $this->request->all();
        (new UserValidator())->goCheck($params, 'update');
        $service->update($id, $params);
        $this->success();
    }

    /**
     * @Permission
     * @param $id
     * @param UserService $service
     * @return void
     */
    public function read ($id, UserService $service): void {
        $data = $service->findUser($id)->hidden(['password']);
        $this->success($data);
    }

    /**
     * @Permission
     * @param $id
     * @param UserService $service
     * @return void
     */
    public function delete ($id, UserService $service): void {
        $service->delete($id);
        $this->success();
    }
}
